package com.lwm.optional;

import com.lwm.entity.*;
import org.junit.Test;

import java.util.Optional;

/**
 * @author liweimin
 * @date 2020/9/15 11:38
 * @description
 */
public class OptionalTest {
    @Test
    public void test01() {
        Optional<Employee> employee = Optional.of(new Employee());
        //快速定位空指针异常位置
        //Optional<Employee> op = Optional.of(null);
        Employee employee1 = employee.get();
        System.out.println(employee1);
    }

    @Test
    public void test02() {
        Optional<Object> empty = Optional.empty();
        /**
         * 提示 java.util.NoSuchElementException: No value present
         */
        System.out.println(empty.get());

    }

    @Test
    public void test03() {
        Optional<Employee> employee = Optional.ofNullable(new Employee());
        if (employee.isPresent()) {
            System.out.println(employee.get().toString());
            Employee employee1 = employee.orElse(new Employee("d", 21, 32332, Status.FREE));
        }
        employee.orElseGet(() -> new Employee());
    }

    @Test
    public void test04() {
        Optional<Employee> employee = Optional.ofNullable(new Employee("d", 21, 32332, Status.FREE));
        Optional<Object> o = employee.flatMap((employee1 ->
                Optional.of(employee1.getName())
        ));
    }

    @Test
    public void test05() {
        Man man = new Man();
        System.out.println(getGodenessName(man));
    }

    public String getGodenessName(Man man) {
        return man.getGodness().getName();
    }

    public String getGodenessNameNew(Optional<NewMan> man) {
        return man.orElse(new NewMan())
                .getGodness()
                .orElse(new Godness("Lily"))
                .getName();
//        return man.getGodness().getName();
    }


    @Test
    public void test06() {

    }
}
